import { SkillItem, sprite, GoodItem } from '../myinterface';
import * as _ from 'lodash';
/**
 * 单个精灵更新，存到精灵组
 * @afterSprite 改变后的精灵
 * @spriteArry 要改变的精灵组
 */
export const spriteUpdateArry = (afterSprite: sprite, spriteArry: sprite[]) => {
  return spriteArry.map((item) => {
    if (afterSprite.name == item.name) {
      return afterSprite;
    }
    return item;
  });
};
/**
 * 单个技能更新，存到精灵中
 * @afterskill 改变后的技能
 * @sprite 要改变的精灵
 */
export const skillUpdataSprite = (afterskill: SkillItem, sprite: sprite) => {
  let afteritem = _.cloneDeep(afterskill);
  let newsprite = _.cloneDeep(sprite);
  afteritem.count--;
  const skill = sprite.skill.map((item) => {
    if (afterskill.name == item.name) {
      return afterskill;
    }
    return item;
  });
  newsprite.skill = skill;
  return newsprite;
};
/**
 * @param sprite 随机抛出一个当前精灵的技能
 * 当前用于模拟boss，后期可以自动战斗
 */
export const randomSkill = (sprite: sprite) => {
  return sprite.skill[_.random(0, 3)];
};

/**
 * 物品的增删
 * @goods 物品仓库
 * @goodname 物品名称
 * @goodNum 改变的数量，此处只是相对值，支持1 -1，不支持清零
 */
export const changeGoods = (
  goods: GoodItem[],
  goodname: string,
  goodNum: number,
) => {
  const newgoods: GoodItem[] = goods.map((ite) => {
    if (ite.name == goodname) {
      return {
        ...ite,
        size: ite.size + goodNum,
      };
    }
    return ite;
  });
  return newgoods;
};
